import sys,os

fastafile = sys.argv[1]

chrorderfile = sys.argv[2]

out1 = open("hap1.fasta", "w")
out2 = open("hap2.fasta", "w")

hap1list = []
hap2list = []
for i in open(chrorderfile):
    hap1, hap2 = i.strip().split("\t")
    hap1list.append(hap1)
    hap2list.append(hap2)


seq = "seq"
name = "name"

dic = {}
for i in open(fastafile):
    if i.startswith(">"):
        dic[name] = seq
        name = i.strip().lstrip(">")
        if name in hap1list or name in hap2list:
            print(name)
        else:
            if name.startswith("Hap1_"):
                hap1list.append(name)
            elif name.startswith("Hap2_"):
                hap2list.append(name)
        seq = ""
    else:
        seq += i
        
dic[name] = seq

num = 1
for i in hap1list:
    if num <= 25:
        if num < 10:
            out1.write(f">AcHAChr0{num}\n")
        else:
            out1.write(f">AcHAChr{num}\n")
    else:
        out1.write(f">AcHAscaffold{num}\n")
    out1.write(dic[i])
    num += 1
num = 1
for i in hap2list:
    if num <= 25:
        if num < 10:
            out2.write(f">AcHBChr0{num}\n")
        else:
            out2.write(f">AcHBChr{num}\n")
    else:
        out2.write(f">AcHBscaffold{num}\n")
    out2.write(dic[i])
    num += 1
    